import javax.net.ssl.SSLContext;
import java.util.ArrayList;
import java.util.Scanner;
public class stu {
    public static void main(String[] args) {
        Scanner middle=new Scanner(System.in);
        To:
        while(true) {
            System.out.println("欢迎来到学生管理系统");
            System.out.println("输入0作为结束");
            System.out.println("1.添加学生");
            System.out.println("2.删除学生");
            //1
            //System.out.println("3.查看学生");
            System.out.println("3.修改学生");
            System.out.println("4.打印学生");
            Integer order=middle.nextInt();
            switch(order){
                case 0:break To;
                case 1:stuins();
                break;
                case 2:delete();
                break;
                case 3:change();
                break;
                case 4:print();
                break;
                default:System.out.println("请输入有效指令:");
                break;
            }
        }
        System.out.println("感谢使用！！");
    }

    private static void change() {
        Scanner ac=new Scanner(System.in);
        Student ss=new Student();
        while(true) {
            System.out.println("请输入更改的姓名(0作为退出键)");

            String s = ac.nextLine();

           // s=ac.nextLine();
            if(s.equals("0")) break;
            int index=getindex(s);
            System.out.println("请输入操作：1.更改姓名\n2.更改密码");
            Integer i=ac.nextInt();
            s=ac.nextLine();
            switch(i){
                case 1: System.out.println("请输入更改的姓名:");
                s=ac.nextLine();
                ss.key=list.get(index).key;
                ss.name=s;
                list.set(index,ss);
                System.out.println("更改成功！");
                break;
                case 2:System.out.println("请输入更改的密码:");
                s=ac.nextLine();
                ss.name=s;
                ss.key=list.get(index).key;
                list.set(index,ss);
                System.out.println("更改成功！");
            }
        }
    }

    private static void print() {
        System.out.println("姓名\t\t密码");
        for (int i = 0; i < list.size(); i++) {
            Student ss=list.get(i);
            System.out.println(ss.name+"\t\t"+ss.key);
        }
        
    }


    private static void delete() {
        System.out.println("输入-1退出");
        Scanner middle=new Scanner(System.in);
        while(list.size()>0){
            String s= middle.nextLine();
            int index=getindex(s);
            if(index==-1) break;
            list.remove(index);
            print();
            System.out.println("删除成功！");
        }
    }

    private static Integer getindex(String s) {
        for (int i = 0; i < list.size(); i++) {
            Student ss=list.get(i);
            if(ss.getName().equals(s)) return i;
        }
        return -1;
    }

    static ArrayList<Student> list=new ArrayList<>();
    private static void stuins() {
        Scanner mi=new Scanner(System.in);
        System.out.println("输入0退出");
        To:
        for (int i = 0;; i++) {
            System.out.println("请输入姓名:");
            String name= mi.nextLine();
            if(name.equalsIgnoreCase("0")) break To;
            System.out.println("请输入密码:");
            String key=mi.nextLine();
            Student li=new Student(name,key);
            list.add(li);
        }
    }

}
